package simple.utils.data.leveldb;

import org.iq80.leveldb.Options;
import simple.utils.data.serializers.Serializer;

import java.io.File;
import java.io.IOException;

/**
 * Created by rehak on 4/25/14.
 */
public class LevelDBFactory {

    public static <K, V> AbstractLevelDB<K, V> create(File dir, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException {
        return create(dir, null, keySerializer, valueSerializer);
    }

    public static <K, V> AbstractLevelDB<K, V> create(File dir, Options options, final Serializer<K> keySerializer, final Serializer<V> valueSerializer) throws IOException {
        return new AbstractLevelDB<K, V>(dir, AbstractLevelDB.ImplType.JNI, options) {
            @Override
            protected byte[] serializeKey(K k) throws IOException {
                return keySerializer.toBytes(k);
            }

            @Override
            protected byte[] serializeValue(V v) throws IOException {
                return valueSerializer.toBytes(v);
            }

            @Override
            protected K deserializeKey(byte[] key) throws IOException {
                return keySerializer.fromBytes(key);
            }

            @Override
            protected V deserializeValue(byte[] value) throws IOException {
                return valueSerializer.fromBytes(value);
            }
        };
    }
}
